package com.example.Controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.Service.RoleService;
import com.example.Service.UserService;
import com.example.pojo.SysRole;
import com.example.pojo.SysUser;
import com.example.pojo.Vo.SysUserVo;
import com.example.util.RedisUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.mgt.SecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController extends RedisUtils {

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/login")
    public String demo(Model model) {

        return "login";
    }

    @RequestMapping("/403")
    public String error(Model model) {

        return "403";
    }

    @RequestMapping("/doLogin")
    public String doLogin(String usrName, String usrPassword, Boolean remeberMe, Model model, HttpSession session) {
//        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>();
//        queryWrapper.lambda().eq(SysUser::getUsrName,usrName).eq(SysUser::getUsrPassword,usrPassword);

//        SysUser user =userService.getOne(queryWrapper);


//        SysUserVo user = userService.getLogin(usrName,usrPassword);
//        if(user!=null){
//            System.out.println(checkbox);
//            if(checkbox!=null){
//                //将我们用户的数据保存到 Redis当中
//                stringRedisTemplate.opsForValue().set("loginUser", JSON.toJSONString(user));
//            }
//            System.out.println( user.getRole().getRoleName());
//            session.setAttribute("loginUser",user);
//            return "main";
//        }else {
//            model.addAttribute("message","用户或密码输入错误");
//            return "login";
//        }



//        Md5Hash md5Hash = new Md5Hash(usrPassword,"czkt",2);

        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(usrName,usrPassword);

        try {
            SecurityUtils.getSubject().login(usernamePasswordToken);

            SysUserVo sysUserVo = (SysUserVo) SecurityUtils.getSubject().getPrincipal();



            session.setAttribute("loginUser", sysUserVo);
        } catch (UnknownAccountException e) {
            model.addAttribute("message", "用户账号不存在");
            return "login";
        } catch (LockedAccountException e) {
            model.addAttribute("message", "账号被禁用");
            return "login";
        } catch (AuthenticationException e) {
            model.addAttribute("message", "账号或密码错误");
            return "login";
        }

        return "main";

    }

    @RequestMapping("/logout")
    public Object logout(HttpSession session) {
        session.removeAttribute("loginUser");
        return "login";
    }
}
